package com.yinhai.ta404.component.security.base.menu;

import com.yinhai.ta404.component.security.base.access.IMenuStore;
import com.yinhai.ta404.core.security.ISecurityConstants;
import com.yinhai.ta404.core.security.vo.BaseResourceVo;
import com.yinhai.ta404.core.security.vo.RestUrlVo;
import org.springframework.security.core.Authentication;
import org.springframework.security.web.FilterInvocation;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.*;


@Component
public class SessionMenuStore implements IMenuStore {
    @Override
    @SuppressWarnings("unchecked")
    public List<BaseResourceVo> getBaseResourceVos(Authentication authentication, Object object) {
        HttpServletRequest request = ((FilterInvocation) object).getRequest();
        HttpSession session = request.getSession(false);
        if (session != null) {
            return (List<BaseResourceVo>) session.getAttribute(ISecurityConstants.CURRENT_MENU);
        }
        return null;
    }

    @Override
    public List<BaseResourceVo> getResourceNoLogin(Authentication authentication, Object object) {
        HttpServletRequest request = ((FilterInvocation) object).getRequest();
        HttpSession session = request.getSession(false);
        if (session != null) {
            return (List<BaseResourceVo>) session.getAttribute(ISecurityConstants.PERMIT_MENU);
        }
        return null;
    }

    @Override
    public List<BaseResourceVo> getResourceLogin(Authentication authentication, Object object) {
        HttpServletRequest request = ((FilterInvocation) object).getRequest();
        HttpSession session = request.getSession(false);
        if (session != null) {
            return (List<BaseResourceVo>) session.getAttribute(ISecurityConstants.LOGIN_PERMIT_MENU);
        }
        return null;
    }

    @Override
    public List<RestUrlVo> getBtnResource(Authentication authentication, Object object, String resourceId) {
        HttpServletRequest request = ((FilterInvocation) object).getRequest();
        HttpSession session = request.getSession(false);
        if (session != null) {
            Map<String, Set<RestUrlVo>> map =  (Map<String, Set<RestUrlVo>>) session.getAttribute(IMenuStore.BTN_AUTH_KEY);
            if(map == null){
                return new ArrayList<>(0);
            }else {
                return new ArrayList<>(map.getOrDefault(resourceId, new HashSet<>(0)));
            }
        }
        return new ArrayList<>(0);
    }

    @Override
    public List<RestUrlVo> getResourceAuthInfo(Authentication authentication, Object object, String resourceId) {
        HttpServletRequest request = ((FilterInvocation) object).getRequest();
        HttpSession session = request.getSession(false);
        if (session != null) {
            Map<String, Set<RestUrlVo>> map =  (Map<String, Set<RestUrlVo>>) session.getAttribute(IMenuStore.RESOURCE_AUTH_KEY);
            if(map == null){
                return new ArrayList<>(0);
            }else {
                return new ArrayList<>(map.getOrDefault(resourceId, new HashSet<>(0)));
            }
        }
        return new ArrayList<>();
    }

    @Override
    public List<String> getBtnNoAuthUrl(Authentication authentication, Object object, String resourceId) {
        HttpServletRequest request = ((FilterInvocation) object).getRequest();
        HttpSession session = request.getSession(false);
        if (session != null) {
            Map<String, Set<String>> map =  (Map<String, Set<String>>) session.getAttribute(IMenuStore.BTN_NO_AUTH_KEY);
            if(map == null){
                return new ArrayList<>(0);
            }else {
                return new ArrayList<>(map.getOrDefault(resourceId, new HashSet<>(0)));
            }
        }
        return new ArrayList<>(0);
    }
}
